package com.example.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.domain.User;



@Repository
public interface UserMapper extends BaseMapper<User>{

    /**
     * 查询所有
     * @return
     */
    @Select("select * from user")
    @Results(@Result(property = "createTime",column = "create_time"))
    List<User> findAll();

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Select("select * from user where id = #{id}")
    @Results(@Result(property = "createTime",column = "create_time"))
    User findById(Integer id);

    /**
     * 更新
     * @param user
     */
    @Update("update user set create_time=#{createTime},avatarUrl=#{avatarUrl},"
    		+ "city=#{city},country=#{country},gender=#{gender},language=#{language},nickName=#{nickName},province=#{province}"
    		+ " where openid=#{openid}")
    void update(User user);

    /**
     * 删除
     * @param id
     */
    @Delete("delete from user where id=#{id}")
    void delete(Integer id);

    /**
     * 增加
     * @param user
     */
    @Insert("insert into user(openid,session_key,create_time,avatarUrl,city,country,"
    		+ "gender,language,nickName,province) values(#{openid},#{session_key},"
    		+ "#{createTime},#{avatarUrl},#{city},"
    		+ "#{country},#{gender},#{language},#{nickName},#{province})")
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
    int add(User user);
    
    /**
     * 根据name查询
     * @param name
     * @return
     */
    @Select("select * from user where nickName = #{name}")
    @Results(@Result(property = "nickName",column = "name"))
    User findByName(String  name);
    
    
}
